<?php
/**
 * 咨询机构
 *
 * User: kaikaic
 * Date: 2018/10/30
 * Time: 16:42
 */

namespace app\psy\controller;

use app\admin\service\CityService;
use app\psy\service\OrganizationService;
use controller\BasicAdmin;
use service\ConfigService;
use think\Db;

class Organization extends BasicAdmin {

    protected $table = "PsyOrganization";

    protected $categoryKey = "organization";

    public function index() {
        $this->title = "咨询机构";

        $query = array();
        $query['name'] = $this->request->get("name");
//        $query['status'] = intval($this->request->get("status", ''));
        $query['category_id'] = intval($this->request->get("category_id"));

        $db = Db::name($this->table);
        if ($query['name']) {
            $db->whereLike("name", "%{$query['name']}%");
        }
        if ($query['category_id']) {
            $db->where(['category_id'=>$query['category_id']]);
        }
//        if ($query['status']) {
//            $db->where(['status'=>$query['status']]);
//        }

        return parent::listRecord($db, "", true, "id desc");
    }

    /**
     * 列表数据处理
     */
    protected function _index_data_filter(&$data) {
        $size = count($data);
        $config = ConfigService::statusConfig();
        $category = $this->readCategoryConfig();

        for ($i=0; $i<$size; $i++) {
            $data[$i]['category'] = $category[$data[$i]['category_id']];
            $data[$i]['status_txt'] = $config[$data[$i]['status']];
        }

        $this->assign('category_list', $category);
    }

    /**
     * 咨询机构-添加
     */
    public function add() {
        if ($this->request->isGet()) {
            $vo = array();
            return view('form', [
                'title'=>'新增咨询机构',
                'vo'=>$vo,
                'image'=>ConfigService::defaultImage(),
                'category'=>$this->readCategoryConfig(),
                'price_type'=>$this->readCategoryConfig("psy_price"),
                'area_list'=>json_encode(CityService::getInstance()->listCityArray())
            ]);
        }

        if ($this->request->isPost()) {
            $data = $this->request->post();

            if ($this->doInsertAction($data)) {
                $this->showSuccess('新增成功！', '/psy/organization/index');
            }else {
                $this->error('新增失败，请稍候再试！');
            }
        }
    }

    /**
     * 咨询机构-编辑
     */
    public function edit() {
        if ($this->request->isGet()) {
            $id = $this->request->get('id', '');
            if (empty($id)) {
                $this->error('参数错误，请稍候再试！');
            }

            $org = OrganizationService::getInstance()->find($id);
            if (empty($org)) {
                $this->error('记录不存在，请重新操作!');
            }

            return view('form', [
                'title'=>'编辑咨询机构',
                'vo'=>$org,
                'image'=>ConfigService::defaultImage(),
                'category'=>$this->readCategoryConfig(),
                'price_type'=>$this->readCategoryConfig("psy_price"),
                'area_list'=>json_encode(CityService::getInstance()->listCityArray())
            ]);
        }

        if ($this->request->isPost()) {
            $id = $this->request->post('id', '');
            if (empty($id)) {
                $this->error('参数错误，请稍候再试！');
            }

            $data = $this->request->post();
            if ($this->doInsertAction($data)) {
                $this->showSuccess('编辑成功！', '/psy/organization/index');
            }else {
                $this->error('编辑失败，请稍候再试！');
            }
        }
    }

    /**
     * 团体单位-删除
     */
    public function remove() {
        $id = intval($this->request->post('id', ''));
        if (empty($id)) {
            $this->error('参数错误!');
        }

        $org = OrganizationService::getInstance()->find($id);
        if (empty($org)) {
            $this->error('记录不存在，请重新操作!');
        }

        $result = OrganizationService::getInstance()->remove(array('id'=>$id));
        if ($result) {
            $this->success('删除成功!', '');
        }else {
            $this->error('删除失败，请重新再试!');
        }
    }

    /**
     * 数据写入
     */
    protected function doInsertAction($data) {
        if (empty($data['id'])) {
            $data['sort'] = OrganizationService::getInstance()->getMaxSort() + 1;
            $data['status'] = 1;
            $data['create_time'] = time();

            $result = OrganizationService::getInstance()->insert($data);
        } else {
            $id = intval($data['id']);
            $result = OrganizationService::getInstance()->update($data, $id);
        }

        return $result;
    }
    public function changeTabelVal() {
        $id = intval($this->request->get('id', ''));
        if (empty($id)) {
            $this->error('参数错误，请重新操作删除!');
        }
        $value = $this->request->post('value',0);
        $field = $this->request->post('field','');
        Db::name($this->table)->where('id', $id)->update(array($field=>$value));
        $this->success('操作成功!', '');
    }
}

